//
//  EGAnimationViewController.m
//  Orchard
//
//  Created by 黄勇锋 on 16/8/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGAnimationViewController.h"

@interface EGAnimationViewController ()

@property (strong,nonatomic) NSMutableArray <CALayer *> * animationLayers;

@property (strong,nonatomic) CALayer *transitonLayer;

@property (strong,nonatomic) CALayer *animationBigLayers;


@end

@implementation EGAnimationViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
}



- (void)addProductsAnimationWithImageView:(UIImageView *)imageView{
    
    //转换frame 的 参照
    CGRect frame = [imageView convertRect:imageView.bounds toView:self.view];
    
    CALayer *transitionLayer = [CALayer layer];
    transitionLayer.frame = frame;
    transitionLayer.contents = imageView.layer.contents;
    
    
    
    [self.view.layer addSublayer:transitionLayer];
    
    [self.animationLayers addObject:transitionLayer];
    
    
    CGPoint p1 = transitionLayer.position;
    CGPoint p3 = CGPointMake(self.view.frame.size.width - self.view.frame.size.width / 4 - self.view.frame.size.width / 8 - 6, self.view.layer.bounds.size.height - 40);
    
    CAKeyframeAnimation *positionAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef pathRef =  CGPathCreateMutable();
    
    CGPathMoveToPoint(pathRef, nil, p1.x, p1.y);
    CGPathAddCurveToPoint(pathRef, nil, p1.x, p1.y -30, p3.x, p1.y - 30, p3.x, p3.y);
    positionAnimation.path = pathRef;
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = @1;
    opacityAnimation.toValue   = @0.9;
    opacityAnimation.fillMode = kCAFillModeForwards;
    opacityAnimation.removedOnCompletion = YES;
    
    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.2, 0.2, 1)];
    
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    
    groupAnimation.animations = @[positionAnimation,transformAnimation,opacityAnimation];
    groupAnimation.duration = 0.8;
    groupAnimation.delegate = self;
    
    [transitionLayer addAnimation:groupAnimation forKey:@"cartParabola"];
}



#pragma mark - 代理方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    if (self.animationLayers.count > 0) {
        
        CALayer *transitionLayer = self.animationLayers[0];
        
        transitionLayer.hidden = YES;
        
        [transitionLayer removeFromSuperlayer];
        
        [self.animationLayers removeObjectAtIndex:0];
        
        [self.view.layer removeAnimationForKey:@"cartParabola"];
        
    }
    
    
    if (self.animationLayers.count > 0) {
        
        CALayer *transitionLayer = self.animationLayers[0];
        
        transitionLayer.hidden = YES;
        
        [transitionLayer removeFromSuperlayer];
        
        [self.animationLayers removeObjectAtIndex:0];
        
        [self.view.layer removeAnimationForKey:@"BigShopCarAnimation"];
    }
}


#pragma mark - 代理方法
- (void)animationDidStart:(CAAnimation *)anim{
    
}


- (NSMutableArray<CALayer *> *)animationLayers{
    
    if (_animationLayers == nil) {
        _animationLayers = [NSMutableArray array];
    }
    
    return _animationLayers;
}




@end
